﻿using StudentWeb.DAL;
using StudentWeb.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StudentWeb.DAL;
using StudentWeb.Model;
using StudentWeb.Common;

namespace StudentWeb.BLL
{
    public class BookBO
    {
        #region 根据ID查询单条记录
        public IResult GetBookDateInfo(int ID)
        {
            var res = new BookDateDAO().GetInfo(ID);
            return new DataResult<BookDate>(res);
        }
        #endregion

        #region 书籍信息 添加单条记录
        public bool InsertBookInfo(dynamic data)
        {
            var result = 0;
            var book = new BookDate()
            {
                BookName = data.BookName,
                BookYear = data.BookYear,
            };
            result = new BookDateDAO().Insert(book);
            return result > 0;
        }
        #endregion
        #region 书籍信息 修改单条记录
        public bool UpdateBookInfo(dynamic date)
        {
            int bookID = date.ID;
            var result = 0;
            var record = new BookDateDAO().GetInfo(bookID);
            record.BookName = date.BookName;
            record.BookYear = date.BookYear;
            result = new BookDateDAO().Update(record);
            return result > 0;
        }
        #endregion
        #region 书籍信息 保存单条记录
        public bool SaveBookInfo(dynamic date)
        {
            int bookID = date.ID ?? 0;
            var result = 0;
            if (bookID > 0)
            {
                var record = new BookDateDAO().GetInfo(bookID);
                record.BookName = date.BookName;
                record.BookYear = date.BookYear;
                result = new BookDateDAO().Update(record);
            }
            else if (bookID == 0) 
            {
                var book = new BookDate()
                {
                    BookName = date.BookName,
                    BookYear = date.BookYear,
                };
                result = new BookDateDAO().Insert(book);
            }
            return result > 0;
        }
        #endregion
        #region 书籍信息 删除单条记录
        
        public IResult DeleteBookInfo(int ID)
        {
            int res = new BookDateDAO().Delete(ID);
            if (res > 0)
            {
                return new Result(ResultCode.Success);
            }
            return new Result(ResultCode.Fail);
        }
        #endregion
    }
}
